package com.vicki.music;

import java.util.ArrayList;

import com.vicki.music.MediaUtils.MediaInfo;

import android.content.ContentResolver;
import android.content.Context;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ExpandAdapter extends BaseExpandableListAdapter implements
		DatasetChanged {

	private static final String ExpandAdapterLog = "ExpandAdapter";
	ArrayList<String> group;
	ArrayList<MediaInfo> child;
	Context context;
	LayoutInflater inflater;

	public ExpandAdapter(Context context) {
		this.context = context;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		group = new ArrayList<String>();
		child = new ArrayList<MediaInfo>();
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		
		if (child != null) {
			return child.get(childPosition);
		}
		return null;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
	
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.song, parent, false);
		}
		
		child.clear();
		ContentResolver resolver = context.getContentResolver();
		MediaUtils utils = MediaUtils.getInstense();
		child = (ArrayList<MediaInfo>) utils.find_child_data(resolver,
				MediaStore.Audio.Media.ARTIST, new String[] { group
						.get(groupPosition) });
		if (child != null) {

			final MediaInfo data = child.get(childPosition);
			final TextView tv_song = (TextView) convertView;
			tv_song.setText(data._album);
		}
		
		
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
	
		child.clear();
		ContentResolver resolver = context.getContentResolver();
		MediaUtils utils = MediaUtils.getInstense();
		child = (ArrayList<MediaInfo>) utils.find_child_data(resolver,
				MediaStore.Audio.Media.ARTIST, new String[] { group
						.get(groupPosition) });
		if (child != null) {
			return child.size();
		}
		return 0;
	}

	@Override
	public Object getGroup(int groupPosition) {
		
		return group.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
	
		if (group != null) {
			return group.size();
		}
		return 0;
	}

	@Override
	public long getGroupId(int groupPosition) {
		
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.song, parent, false);
		}

		final String data = group.get(groupPosition);
		final TextView tv_song = (TextView) convertView;
		tv_song.setText(data);
		tv_song.setPadding(33, 0, 15, 0);
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void onDatasetChanged(Object father, Object child) {
		if (father == null)
			return;
		this.group = (ArrayList<String>) father;
		notifyDataSetChanged();

	}
}
